package com.jic.market.mapper.coupon;

import com.jic.market.entity.base.BaseActivity;
import com.jic.market.entity.coupon.CouponGoods;

import com.jic.market.request.PromotionPolymericRequest;
import com.jic.market.request.manager.base.QueryGoodsReuqest;
import com.jic.market.response.PromotionPolymericProductResponse;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.omg.CORBA.StringHolder;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 优惠券商品关联表
 *
 * @author : seven
 * @version : v1.0.0
 * @email : 774329481@qq.com
 * @since : 2020-03-15 08:24:22
 */
@Repository
public interface CouponGoodsMapper {

    long insert(CouponGoods couponGoods);

    long delete(Long promotionId);

    long deleteByPromotionId(String promotionId);

    long update(CouponGoods couponGoods);

    long updateMutexFlag(CouponGoods couponGoods);

    CouponGoods queryByPrimaryKey(Long id);

    List<CouponGoods> query(CouponGoods couponGoods);

    List<CouponGoods> queryPage(@Param("startIndex") int startIndex, @Param("limit") int limit, @Param("couponGoods") CouponGoods couponGoods);

    long queryPageCount(CouponGoods couponGoods);

    @Select({
            "<script>",
            "select * from market_coupon_goods where promotion_id=#{promotionId} and delete_flag='0'",
            "<trim suffixOverrides=\"and\"> ",
            "<if test=\"start !=null and limit != null\"> ",
            "   limit #{start},#{limit}",
            "</if>  ",
            "</trim>",
            "</script>"
    })
    List<CouponGoods> queryByPromotionId(@Param("promotionId")String promotionId ,@Param("start") Integer start ,@Param("limit") Integer limit);

    @Update("update market_coupon_goods set delete_flag='1' where promotion_id=#{promotionId} and " +
            " merchant_id=#{merchantId} and delete_flag='0'")
    long deleteGoodsByPromotionIdAndMerchantId(@Param("promotionId") String promotionId,
                                               @Param("merchantId") String merchantId);

    @Update("update market_coupon_goods set delete_flag='1' where promotion_id=#{promotionId} and " +
            " store_id=#{storeId} and delete_flag='0'")
    long deleteGoodsByPromotionIdAndStoreId(@Param("promotionId") String promotionId,
                                            @Param("storeId") String storeId);

    @Update("update market_coupon_goods set delete_flag='1' where promotion_id=#{promotionId} and " +
            " goods_id=#{goodsId} and delete_flag='0'")
    long deleteGoodsByPromotionIdAndGoodsId(@Param("promotionId") String promotionId,
                                            @Param("goodsId") String goodsId);
    @Select("select count(1) from market_coupon_goods where promotion_id=#{promotionId} and delete_flag='0'")
    Long queryByPromotionIdTotal(@Param("promotionId") String promotionId);

    @Delete("delete from market_coupon_goods where promotion_id=#{promotionId}")
    void deleteGoodsByPromotionId(@Param("promotionId") String promotionId);

    @Select("select count(1) from  market_coupon_goods where promotion_id=#{promotionId} and spu_id != '0' and delete_flag='0'")
    Long querySkuTotalByPromotionId(@Param("promotionId") String promotionId);

    List<CouponGoods> queryByPromotionIdBySpu(@Param("promotionId") String promotionId, @Param("start") Integer startIndex, @Param("limit") Integer limit, @Param("queryGoodsReuqest") QueryGoodsReuqest queryGoodsReuqest);

    Long queryByPromotionIdTotalBySpu(@Param("promotionId") String promotionId, @Param("queryGoodsReuqest") QueryGoodsReuqest queryGoodsReuqest);

    @Select("select count(1) from market_coupon_goods where promotion_id=#{promotionId} and delete_flag='0'")
    Long queryCouponGoodsTotal(@Param("promotionId") String promotionId);

    List<String> querySkuIdByPromotionIdAndSkuIds(@Param("promotionId") String promotionId, @Param("goodsIds") List<String> goodsIds);

    List<BaseActivity> queryDoMutex(@Param("goodsId") Long goodsId,@Param("spuOrSku") Boolean spuOrSku);

    List<CouponGoods> queryRedByPromotionIdBySpu(@Param("promotionId") String promotionId, @Param("mutexFlag") Integer mutexFlag, @Param("start") Integer startIndex, @Param("limit") Integer limit ,@Param("queryGoodsReuqest") QueryGoodsReuqest queryGoodsReuqest);

    Long queryRedByPromotionIdTotalBySpu(@Param("promotionId") String promotionId,@Param("mutexFlag") Integer mutexFlag,@Param("queryGoodsReuqest") QueryGoodsReuqest queryGoodsReuqest);

    @Select("select count(1) from market_coupon_goods where promotion_id=#{promotionId} and mutex_flag = #{mutexFlag} and delete_flag='0' and spu_id != '0'")
    Long queryRedSkuTotalByPromotionId(@Param("promotionId") String promotionId,@Param("mutexFlag") Integer mutexFlag);

    List<PromotionPolymericProductResponse> queryPromotionPolyProductList(@Param("request") PromotionPolymericRequest request);
}